/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MiniForm.java
 *
 * Created on Apr 15, 2009, 3:33:04 PM
 */

package forms;

import javax.swing.event.EventListenerList;
import core.XEvent;
import core.XEventListener;
import utils.LanguageManager;

/**
 *
 * @author Hung Cuong <nhc.hcmuns at gmail.com>
 */
public class MiniForm extends javax.swing.JFrame {
    protected javax.swing.event.EventListenerList listenerList = new EventListenerList();

    public void addMyEventListener(XEventListener evt) {
        listenerList.add(XEventListener.class, evt);
    }

    public void removeMyEventListener(XEventListener evt) {
        listenerList.remove(XEventListener.class, evt);
    }
    
    /** Creates new form MiniForm */
    public MiniForm() {
        initComponents();

        getRootPane().setDefaultButton(btnOK);  // set default button
        setLocationRelativeTo(this);            // center the form
    }

    public MiniForm(LanguageManager langManager){

        initComponents();
        btnOK.setText(langManager.TranslateLang("btnOK"));
        btnCancel.setText(langManager.TranslateLang("btnCancel"));

        getRootPane().setDefaultButton(btnOK);  // set default button
        setLocationRelativeTo(this);            // center the form
    }

    public void setLabelText(String text) {
        lblText.setText(text);
    }

    public void setTextboxText(String text) {
        txtPath.setText(text);
        txtPath.selectAll();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblText = new javax.swing.JLabel();
        txtPath = new javax.swing.JTextField();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tiny Total Commander");
        setResizable(false);

        lblText.setText("Label Text");

        btnOK.setText("OK");
        btnOK.setMaximumSize(new java.awt.Dimension(67, 23));
        btnOK.setMinimumSize(new java.awt.Dimension(67, 23));
        btnOK.setPreferredSize(new java.awt.Dimension(67, 23));
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblText)
                    .addComponent(txtPath, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblText)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        XEvent ev = new XEvent(txtPath.getText());
        Object[] listeners = listenerList.getListenerList();

        // Each listener occupies two elements - the first is the listener class
        // and the second is the listener instance
        for (int i = 0; i < listeners.length; i += 2) {
            if (listeners[i] == XEventListener.class) {
                ((XEventListener)listeners[i+1]).myEventOccurred(ev);
            }
        }

        this.dispose(); // close form
}//GEN-LAST:event_btnOKActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose(); // close form
}//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JLabel lblText;
    private javax.swing.JTextField txtPath;
    // End of variables declaration//GEN-END:variables

}
